<?php

namespace app\common\command;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class UserFinance extends Command
{
    protected function configure()
    {
        $this->setName('userFinance')->setDescription('每日利息');
    }

    protected function execute(Input $input, Output $output)
    {
        $minMoney = config('site.min_money');//门槛
        $yearRate = config('site.year_rate');//年利率
        $userModel = new User();
        $userData = $userModel->where('wallet','>=',$minMoney)->select();
        if($userData && $yearRate > 0){
            //年利率÷365天×日金额＝日息
            $res = false;
            Db::startTrans();
            try {
                foreach($userData as $k=>$v){
                    $userFinanceModel = new \app\common\model\UserFinance();
                    $count = $userFinanceModel->where('user_id', $v->id)->whereTime('createtime','d')->count();
                    if(!$count){
                        $userFinanceModel->user_id = $v->id;
                        $chu = $yearRate / 365;
                        $userFinanceModel->interest = bcmul($chu,$v->wallet,2);
                        $res =  $userFinanceModel->save();
                    }
                }
                if($res){
                    Db::commit();
                    echo '更新成功';
                }else{
                    Db::rollback();
                    echo '无数据';
                }
            }catch (\Throwable $e){
                Db::rollback();
                echo $e->getMessage();
            }
        }
    }
}
